package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.service.*;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public BusinessDataVO businessDate() {
        // 获取今天的时间
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);
        return businessDate(start, end);
    }

    @Override
    public BusinessDataVO businessDate(LocalDateTime start, LocalDateTime end) {
        // 查询营业额，状态是已经完成的订单的总金额
        Double turnover = orderService.getTurnover(start, end);
        // 查询有效订单数
        int validOrderCount = orderService.countValidOrders(start, end);
        // 查询总订单数
        int totalOrders = orderService.countTotalOrders(start, end);
        // 计算完成率
        Double orderCompletionRate = (totalOrders<=0? 0: validOrderCount * 1.0 / totalOrders);
        // 计算平均客单价
        Double unitPrice = (validOrderCount<=0? 0 : turnover / validOrderCount);
        // 查询新增用户数
        int newUsers = userService.countNewUser(start, end);
        //封装返回
        BusinessDataVO vo = new BusinessDataVO();
        vo.setTurnover(turnover);
        vo.setNewUsers(newUsers);
        vo.setUnitPrice(unitPrice);
        vo.setValidOrderCount(validOrderCount);
        vo.setOrderCompletionRate(orderCompletionRate);
        return vo;
    }

    @Override
    public OrderOverViewVO orderOverView() {
        // 获取今天的时间
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);
        List<StatusCountVO> voList = orderService.groupCountByStatusAndCreateTime(start, end);
        if(CollectionUtils.isEmpty(voList)){
            return new OrderOverViewVO();
        }
        // 封装返回
        OrderOverViewVO vo = new OrderOverViewVO();
        // 所有
        vo.setAllOrders(voList.stream().map(StatusCountVO::getCount).reduce(0, (a, b)->a+b));
        // 待接单
        vo.setWaitingOrders(getCountByStatus(voList, Orders.TO_BE_CONFIRMED));
        // 待派送
        vo.setDeliveredOrders(getCountByStatus(voList, Orders.CONFIRMED));
        // 已完成
        vo.setCompletedOrders(getCountByStatus(voList, Orders.COMPLETED));
        // 已取消
        vo.setCancelledOrders(getCountByStatus(voList, Orders.CANCELLED));
        return vo;
    }

    private int getCountByStatus(List<StatusCountVO> voList, int status){
        Optional<Integer> first = voList.stream()
                // 元素过滤
                .filter(vo -> Objects.equals(vo.getStatus(), status))
                // 数据转换
                .map(vo-> vo.getCount())
                // 查找第一个满足条件的记录
                .findFirst();
        if(first.isPresent()){
            return first.get();
        } else{
            return 0;
        }
    }

    @Override
    public DishOverViewVO overviewDishes() {
        List<StatusCountVO> voList = dishService.groupCountByStatus();
        if(CollectionUtils.isEmpty(voList)){
            return new DishOverViewVO();
        }
        DishOverViewVO vo = new DishOverViewVO();
        // 在售
        vo.setSold(getCountByStatus(voList, StatusConstant.ENABLE));
        // 售罄
        vo.setDiscontinued(getCountByStatus(voList, StatusConstant.DISABLE));
        return vo;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        List<StatusCountVO> voList = setmealService.groupCountByStatus();
        if(CollectionUtils.isEmpty(voList)){
            return new SetmealOverViewVO();
        }
        SetmealOverViewVO vo = new SetmealOverViewVO();
        // 在售
        vo.setSold(getCountByStatus(voList, StatusConstant.ENABLE));
        // 售罄
        vo.setDiscontinued(getCountByStatus(voList, StatusConstant.DISABLE));
        return vo;
    }
}
